package com.cn.ant.modules.wechat.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.DateUtils;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.sys.entity.User;
import com.cn.ant.modules.sys.utils.UserUtils;
import com.cn.ant.modules.wechat.entity.WeiXinNewListType;
import com.cn.ant.modules.wechat.entity.WeixinAccount;
import com.cn.ant.modules.wechat.service.WeiXinNewListTypeService;
import com.cn.ant.modules.wechat.service.WeixinAccountServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 消息列表类型Controller
 * @author AntDream
 * @version 2015-07-30
 */
@Controller
@RequestMapping(value = "${adminPath}/wechat/weiXinNewListType")
public class WeiXinNewListTypeController extends BaseController {

	@Autowired
	private WeiXinNewListTypeService weiXinNewListTypeService;
    @Autowired
    private WeixinAccountServiceImpl accountService;
	
	@ModelAttribute
	public WeiXinNewListType get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return weiXinNewListTypeService.get(id);
		}else{
			return new WeiXinNewListType();
		}
	}
	
	@RequiresPermissions("wechat:weiXinNewListType:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeiXinNewListType weiXinNewListType, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()) {
			weiXinNewListType.setUserid(user.getId());
		}
		Map<String,Object> param = new HashMap<String,Object>();
        Page<WeiXinNewListType> page = weiXinNewListTypeService.find(new Page<WeiXinNewListType>(request, response), param);
        model.addAttribute("page", page);
		return "modules/wechat/weiXinNewListTypeList";
	}

	@RequiresPermissions("wechat:weiXinNewListType:view")
	@RequestMapping(value = "form")
	public String form(WeiXinNewListType weiXinNewListType, Model model) {
		model.addAttribute("weiXinNewListType", weiXinNewListType);
        List<WeixinAccount> accountList = accountService.findByLoginUser();
        model.addAttribute("accountList", accountList);
        return "modules/wechat/weiXinNewListTypeForm";
	}

	@RequiresPermissions("wechat:weiXinNewListType:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(WeiXinNewListType weiXinNewListType, Model model) {
        User user = UserUtils.getUser();
        AjaxJson jo = new AjaxJson();
        try {
            if (!beanValidator(model, weiXinNewListType)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
            } else {
                if (StringUtils.isBlank(weiXinNewListType.getId())) {
                    weiXinNewListType.setId(Identities.generateUUID());
                    weiXinNewListType.setOfficeid(user.getOffice().getId());
                    weiXinNewListType.setUserid(user.getId());
                    weiXinNewListType.setUpdateTime(DateUtils.getCurrDate());
                    weiXinNewListType.setHits(0);
                    weiXinNewListTypeService.save(weiXinNewListType);
                } else {
                    weiXinNewListType.setUpdateTime(DateUtils.getCurrDate());
                    weiXinNewListTypeService.update(weiXinNewListType);
                }
            }
            msg = "保存消息列表类型'" + weiXinNewListType.getTypeName() + "'成功";
            jo.setMsg(msg);
        } catch (Exception e) {
            e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
        }
        return jo;
    }

    @RequiresPermissions("wechat:weiXinNewListType:edit")
	@RequestMapping(value = "delete")
    @ResponseBody
	public AjaxJson delete(String id, RedirectAttributes redirectAttributes) {
        AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(id)) {
            weiXinNewListTypeService.delete(id);
            jo.setMsg("删除成功,页面在2s后跳转..");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

}
